﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Web.Configuration;

namespace ForestryEnterprise
{
    public partial class AssignTransporterToEmployee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                fillDropList();
            }

        }
        private void fillDropList()
        {

            
                this.empList.Items.Add(new ListItem("Select"));
                string statement = "select ssn, p_fname from person, employee where ssn=p_id";
                OleDbConnection connection = new OleDbConnection(WebConfigurationManager.ConnectionStrings["forestryDB"].ConnectionString);
                OleDbCommand command = new OleDbCommand(statement, connection);

                connection.Open();
                OleDbDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    ListItem newEmp = new ListItem(reader["p_fname"].ToString(), reader["ssn"].ToString());
                    this.empList.Items.Add(newEmp);
                }

                reader.Close();
                connection.Close();

                this.equList.Items.Add(new ListItem("Select"));
                string statement1 = "select eq_id, eq_name from Equipment where eq_used='No' and eq_status='Good condition'";
                OleDbCommand command1 = new OleDbCommand(statement1, connection);

                connection.Open();
                OleDbDataReader reader1 = command1.ExecuteReader();

                while (reader1.Read())
                {
                    ListItem newEqu = new ListItem(reader1["eq_name"].ToString(), reader1["eq_id"].ToString());
                    this.equList.Items.Add(newEqu);
                }

                reader1.Close();
                connection.Close();
        }
        private void updateDataView1()
        {
            string statement = "select ssn, p_fname, p_lname, p_birthDate, a_zip, a_city, a_state, a_strname, a_strnumb from person where ssn= " + this.empList.SelectedValue;
            this.EmpData.SelectCommand = statement;
            this.EmpGridView.DataBind();
        }
        private void updateDataView2()
        {
            string statement = "select * from equipment where eq_id= "+this.equList.SelectedValue;
            this.EquData.SelectCommand = statement;
            this.EquGridView.DataBind();
        }

        protected void empList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.empList.SelectedIndex > 0)
            {
                updateDataView1();
            }
        }

        protected void equList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.equList.SelectedIndex > 0)
            {
                updateDataView2();
            }
        }

        protected void EmpGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Empssn.Text = this.EmpGridView.SelectedRow.Cells[1].Text;
            this.empFname.Text = this.EmpGridView.SelectedRow.Cells[2].Text;
            this.EmpLname.Text = this.EmpGridView.SelectedRow.Cells[3].Text;
        }

        protected void EquGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.EquID.Text = this.EquGridView.SelectedRow.Cells[1].Text;
            this.Eq_name.Text = this.EquGridView.SelectedRow.Cells[2].Text;
            
        }

        protected void Assign_Click(object sender, EventArgs e)
        {
            string statement = "update Equipment set eq_used='Yes' where eq_id = " + this.EquGridView.SelectedRow.Cells[1].Text;
            OleDbConnection connection = new OleDbConnection(WebConfigurationManager.ConnectionStrings["forestryDB"].ConnectionString);
            OleDbCommand command = new OleDbCommand(statement, connection);
            fillDropList();
            updateDataView1();
            updateDataView2();
        }





    }
}